﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLNhaHangTiecCuoi.Models;
using System.Threading.Tasks;

namespace QLNhaHangTiecCuoi.Controllers
{
    public class HopDongsController : Controller
    {
        private QLNhaHangTCEntities db = new QLNhaHangTCEntities();

        // page index hopdong
        public ActionResult Index()
        {
            var hopdongs = db.HopDongs.Include(h => h.KhachHang).Include(h => h.Sanh);
            return View(hopdongs.ToList());
        }


        // page details hopdong
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HopDong hopDong = db.HopDongs.Find(id);
            if (hopDong == null)
            {
                return HttpNotFound();
            }
            return View(hopDong);
        }
        //create MaHD
        public string CreateMaHD()
        {
            var item = db.HopDongs.OrderByDescending(i => i.MaHD).FirstOrDefault();
            string ma;
            if (item == null) { ma = "HD0000"; }
            else ma = item.MaHD;
            int so = Convert.ToInt32(ma.Substring(2, 4));
            so++;
            string MaHD = "HD" + so.ToString("0000");
            return MaHD;
        }
        //GET: HopDongs/Create
        public ActionResult Create()
        {
            return View();
        }
        // POST: HopDongss/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "CMNDKH,TenKH,Email,DiaChi,SDT,GhiChu")] KhachHang khachHang)
        {
            KhachHang KH = new KhachHang()
            {
                CMNDKH = khachHang.CMNDKH,
                TenKH = khachHang.TenKH,
                DiaChi = khachHang.DiaChi,
                Email = khachHang.Email,
                SDT = khachHang.SDT,
                GhiChu = khachHang.GhiChu
            };
            HopDong hopdong = new HopDong()
            {
                MaHD = CreateMaHD(),
                CMNDKH=khachHang.CMNDKH
            };
            if (ModelState.IsValid)
            {
                db.HopDongs.Add(hopdong);
                db.KhachHangs.Add(KH);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(khachHang);
        }

        // GET: HopDongs/Edit/5
        public ActionResult Edit(string id,string id1)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HopDong hopDong = db.HopDongs.Find(id);
            if (hopDong == null)
            {
                return HttpNotFound();
            }
            ViewBag.ChonSanh = GetSanh();
            return View(hopDong);
        }

        // POST: HopDongs/Edit/MaHD
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "MaHD,CMNDKH,MaSanh,NgayTaoHD,TenChuRe,TenCoDau,NgayCuoi,GhiChu")] HopDong hopDong)
        {
            if (ModelState.IsValid)
            {
                db.Entry(hopDong).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ChonSanh = GetSanh();
            return View(hopDong);
        }

        // GET: HopDongs/Delete/MaHD
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HopDong hopDong = db.HopDongs.Find(id);
            if (hopDong == null)
            {
                return HttpNotFound();
            }
            return View(hopDong);
        }

        // POST: HopDongs/Delete/MaHD
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            HopDong hopDong = db.HopDongs.Find(id);
            db.HopDongs.Remove(hopDong);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        //get sanh
        private List<SelectListItem> GetSanh()
        {
            var x = (from n in db.Sanhs select n);
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in x)
            {
                items.Add(new SelectListItem { Text = item.TenSanh, Value = item.MaSanh });
            }
            return items;
        }
        //get dich vu
        private List<SelectListItem> GetDichVu()
        {
            var x = (from n in db.DichVus select n);
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in x)
            {
                items.Add(new SelectListItem { Text = item.TenDV, Value = item.MaDV });
            }
            return items;
        }
        //get thuc don
        private List<SelectListItem> GetThucDon()
        {
            var x = (from n in db.ThucDons select n);
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in x)
            {
                items.Add(new SelectListItem { Text = item.TenTD, Value = item.MaTD });
            }
            return items;
        }
        //get mon an
        private List<SelectListItem> GetMonAn()
        {
            var x = (from n in db.MonAns select n);
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in x)
            {
                items.Add(new SelectListItem { Text = item.TenMonAn, Value = item.MaMonAn });
            }
            return items;
        }
        //index hddv page
        public ActionResult IndexHDDV(string id)
        {
            var hopdongdv = db.HopDong_DV.Include(h => h.DichVu).Where(x=>x.MaHD==id);
            ViewBag.ChonHopDong = id;
            return View(hopdongdv);
        }
        // GET: HopDongs/CreateHDDV
        public ActionResult CreateHDDV(string id)
        {
            ViewBag.MaHopDong = id;
            ViewBag.ChonDichVu = GetDichVu();
            return View();
        }
        // POST: HopDongss/CreateHDDV
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CreateHDDV([Bind(Include = "MaHD,MaDV,SoLuong")] HopDong_DV HopDongDV)
        {
            string mahd = HopDongDV.MaHD;
            HopDong_DV hopdongdv = new HopDong_DV()
            {
                 MaHD=mahd,
                 MaDV=HopDongDV.MaDV,
                 SoLuong=HopDongDV.SoLuong
            };
            if (ModelState.IsValid)
            {
                db.HopDong_DV.Add(hopdongdv);
                await db.SaveChangesAsync();
                return RedirectToAction("IndexHDDV/"+mahd);
            }
            ViewBag.ChonDichVu = GetDichVu();
            return View(HopDongDV);
        }
        // GET: HopDongs/EditHĐV/MaHD/MaDV
        public ActionResult EditHDDV(string MaHD,string MaDV)
        {
            HopDong_DV hopdongdv = db.HopDong_DV.Find(MaHD,MaDV);
            if (hopdongdv == null)
            {
                return HttpNotFound();
            }
            ViewBag.MaHopDong = MaHD;
            ViewBag.MaDichVu = MaDV;
            ViewBag.ChonDichVu = GetDichVu();
            return View(hopdongdv);
        }

        // POST: HopDongs/EditHDDV/MaHD/MaDV
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditHDDV([Bind(Include = "MaHD,MaDV,SoLuong")] HopDong_DV hopdongdv)
        {
            string mahd = hopdongdv.MaHD;
            if (ModelState.IsValid)
            {
                db.Entry(hopdongdv).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexHDDV/"+mahd);
            }
            ViewBag.ChonDichVu = GetDichVu();
            return View(hopdongdv);
        }

        // GET: HopDongs/DeleteHDDV/MaHD/MaDV
        public ActionResult DeleteHDDV(string MaHD, string MaDV)
        {
            HopDong_DV hopdongdv = db.HopDong_DV.Find(MaHD, MaDV);
            if (hopdongdv == null)
            {
                return HttpNotFound();
            }
            return View(hopdongdv);
        }

        // POST: HopDongs/DeleteHDDV/MaHD/MaDV
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        //Ham nay chua on
        public ActionResult DeleteHDDVConfirmed(string MaHD, string MaDV)
        {
            HopDong_DV hopdongdv = db.HopDong_DV.Find(MaHD, MaDV);
            db.HopDong_DV.Remove(hopdongdv);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        // GET: HopDongs/IndexHDTD
        public ActionResult IndexHDTD(string id)
        {
            var hopdongtd = db.HopDong_TD.Include(h => h.ThucDon).Where(x => x.MaHD == id);
            ViewBag.ChonHopDong = id;
            return View(hopdongtd);
        }
        // GET: HopDongs/CreateHDTD
        public ActionResult CreateHDTD(string id)
        {
            ViewBag.MaHopDong = id;
            ViewBag.ChonThucDon = GetThucDon();
            return View();
        }
        // POST: HopDongss/CreateHDTD
       
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CreateHDTD([Bind(Include = "MaHD,MaTD,SoLuong")] HopDong_TD HopDongTD)
        {
            string mahd = HopDongTD.MaHD;
            HopDong_TD hopdongtd = new HopDong_TD()
            {
                MaHD = mahd,
                MaTD = HopDongTD.MaTD,
                SoLuong = HopDongTD.SoLuong
            };
            if (ModelState.IsValid)
            {
                db.HopDong_TD.Add(hopdongtd);
                await db.SaveChangesAsync();
                return RedirectToAction("IndexHDTD/"+mahd);
            }
            ViewBag.ChonThucDon = GetThucDon();
            return View(HopDongTD);
        }
        // GET: HopDongs/EditHDTD/MaHD/MaTD
        public ActionResult EditHDTD(string MaTD, string MaHD)
        {
            HopDong_TD hopdongtd = db.HopDong_TD.Find(MaTD, MaHD);
            if (hopdongtd == null)
            {
                return HttpNotFound();
            }
            ViewBag.MaHopDong = MaHD;
            ViewBag.MaThucDon = MaTD;
            ViewBag.ChonThucDon = GetThucDon();
            return View(hopdongtd);
        }

        // POST: HopDongs/EditHDTD/MaHD/MaTD
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditHDTD([Bind(Include = "MaTD,MaHD,SoLuong")] HopDong_TD hopdongtd)
        {
            string mahd = hopdongtd.MaHD;
            if (ModelState.IsValid)
            {
                db.Entry(hopdongtd).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexHDTD/" + mahd);
            }
            ViewBag.ChonDichVu = GetDichVu();
            return View(hopdongtd);
        }

        // GET: HopDongs/DeleteHDTD/MaHD/MaTD
        public ActionResult DeleteHDTD(string MaHD, string MaTD)
        {
            HopDong_TD hopdongtd = db.HopDong_TD.Find(MaHD, MaTD);
            if (hopdongtd == null)
            {
                return HttpNotFound();
            }
            return View(hopdongtd);
        }

        // POST: HopDongs/DeleteHDTD/MaHD/MaTD
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        //ham nay chua on
        public ActionResult DeleteHDTDConfirmed(string MaHD, string MaTD)
        {
            HopDong_TD hopdongtd = db.HopDong_TD.Find(MaHD, MaTD);
            db.HopDong_TD.Remove(hopdongtd);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        // GET: HopDongs/IndexCTTD
        public ActionResult IndexCTTD(string MaHD,string MaTD)
        {
            var chitiettd = db.ChiTietThucDons.Include(h => h.MonAn).Where(x => x.MaTD == MaTD);
            ViewBag.MaThucDon = MaTD;
            ViewBag.MAHopDong = MaHD;
            return View(chitiettd);
        }
        // GET: HopDongs/CreateCTTD
        public ActionResult CreateCTTD(string MaHD,string MaTD)
        {
            ViewBag.ChonMonAn = GetMonAn();
            ViewBag.MaHopDong = MaHD;
            ViewBag.MaThucDon = MaTD;
            ViewBag.ChonThucDon = GetThucDon();
            return View();
        }
        // POST: HopDongss/CreateCTTD

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CreateCTTD([Bind(Include = "MaTD,MaMonAn,SoLuong")] ChiTietThucDon ChiTietTD,string mahd)
        {
            string matd = ChiTietTD.MaTD;
            ChiTietThucDon chitiettd = new ChiTietThucDon()
            {
                MaTD = matd,
                MaMonAn = ChiTietTD.MaMonAn,
                SoLuong = ChiTietTD.SoLuong
            };
            if (ModelState.IsValid)
            {
                db.ChiTietThucDons.Add(chitiettd);
                await db.SaveChangesAsync();
                return RedirectToAction("IndexCTTD", new { MaTD = matd,MaHD = mahd});
            }
            ViewBag.ChonMonAn = GetMonAn();
            return View(ChiTietTD);
        }
        // GET: HopDongs/EditCTTD/MaTD
        public ActionResult EditCTTD(string MaHD,string MaTD,string MaMonAn)
        {
            ChiTietThucDon chitiettd = db.ChiTietThucDons.Find(MaTD,MaMonAn);
            if (chitiettd == null)
            {
                return HttpNotFound();
            }
            ViewBag.MaThucDon = MaTD;
            ViewBag.MaHopDong = MaHD;
            ViewBag.ChonMonAn = GetMonAn();
            return View(chitiettd);
        }

        // POST: HopDongs/EditCTTD/MaTD
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditCTTD([Bind(Include = "MaTD,MaMonAn,SoLuong")] ChiTietThucDon chitiettd,string mahd)
        {
            string matd = chitiettd.MaTD;
            if (ModelState.IsValid)
            {
                db.Entry(chitiettd).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("IndexCTTD", new { MaTD = matd, MaHD = mahd });
            }
            ViewBag.ChonMonAn = GetMonAn();
            return View(chitiettd);
        }

        // GET: HopDongs/DeleteHDTD/MaHD/MaTD
        public ActionResult DeleteCTTD(string MaTD,string MaMonAn)
        {
            ChiTietThucDon chitiettd = db.ChiTietThucDons.Find(MaTD,MaMonAn);
            if (chitiettd == null)
            {
                return HttpNotFound();
            }
            return View(chitiettd);
        }

        // POST: HopDongs/DeleteHDTD/MaHD/MaTD
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        //ham nay chua on
        public ActionResult DeleteCTTDConfirmed(string MaTD, string MaMonAn)
        {
            ChiTietThucDon chitiettd = db.ChiTietThucDons.Find(MaTD, MaMonAn);
            db.ChiTietThucDons.Remove(chitiettd);
            db.SaveChanges();
            return RedirectToAction("IndexCTTD/"+MaTD);
        }
        
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
